package edu.pdx.LAMA.AndroidCourse.scheduler;

import java.util.Calendar;

import edu.pdx.LAMA.AndroidCourse.ModelLayer.EventService;
import edu.pdx.LAMA.AndroidCourse.scheduler.AppCalendarViewFragment.OnDateClickListenerFromCalendar;
import edu.pdx.LAMA.AndroidCourse.scheduler.AppDayViewFragment.OnDateChangeListenerFromDayView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class ScheduleActivity extends FragmentActivity implements
OnDateClickListenerFromCalendar, OnDateChangeListenerFromDayView{

	private static final String SAVER_SELECTED_DATE = "edu.pdx.LAMA.AndroidCourse.scheduler.saverSelectedDate";

	private String selectedDate;
	private TextView calendarTab;
	private TextView dayTab;
	private Button createEvent;
	private static final String TAG = "ScheduleActivity";
	private int day;
	private int month;
	private int year;

	private String saverDate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_schedule);

		if(savedInstanceState!=null){
			selectedDate = savedInstanceState.getString(SAVER_SELECTED_DATE);
			Log.d(TAG, "after: "+selectedDate);
		}

		//EventService eventService = new EventService(this);
		Calendar calendar = Calendar.getInstance();
		day = calendar.get(Calendar.DATE);
		month = calendar.get(Calendar.MONDAY);
		year = calendar.get(Calendar.YEAR);
		createFragment();

		calendarTab = (TextView) findViewById(R.id.calendarTabText);
		calendarTab.setText("Calendar");
		calendarTab.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d(TAG, "on click calendar");
				Bundle bundle = new Bundle();
				if (selectedDate!=null){
					bundle.putString(AppCalendarViewFragment.EXTRA_SELECTED_DATE, selectedDate);
				} else {
					bundle.putString(AppCalendarViewFragment.EXTRA_SELECTED_DATE, day+"/"+month+"/"+year);
					selectedDate = day+"/"+month+"/"+year;
				}

				Fragment frag = new AppCalendarViewFragment();
				frag.setArguments(bundle);
				//getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, frag).addToBackStack(null).commit();
				getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, frag).commitAllowingStateLoss();
				//getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, frag).commit();
			}
		});

		dayTab = (TextView) findViewById(R.id.dayTabText);
		dayTab.setText("Day");
		dayTab.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d(TAG, "on click day");
				Bundle bundle = new Bundle();
				if (selectedDate!=null){
					bundle.putString(AppDayViewFragment.EXTRA_SELECTED_DATE, selectedDate);
				} else {
					bundle.putString(AppDayViewFragment.EXTRA_SELECTED_DATE, day+"/"+month+"/"+year);
					selectedDate = day+"/"+month+"/"+year;
				}

				Fragment frag = new AppDayViewFragment();
				frag.setArguments(bundle);
				//getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, frag).commit();
				//getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, frag).addToBackStack(null).commit();

				getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, frag).commitAllowingStateLoss();
			}
		});

		createEvent = (Button) findViewById(R.id.createEvent);
		createEvent.setText("Create Event");
		// once the button is clicked we need to start the new activity
		createEvent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(), CreateOrModifyEventActivity.class);
				i.putExtra(CreateOrModifyEventActivity.EXTRA_EVENT_ID, 999999);
				i.putExtra(CreateOrModifyEventActivity.EXTRA_EVENT_DATE, selectedDate);
				startActivity(i);
			}
		});
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.schedule, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		} else if (id == R.id.action_add_category) {
			Intent i = new Intent(this, AddCategoryActivity.class);
			startActivity(i);
		}
		return super.onOptionsItemSelected(item);
	}


	protected void createFragment() {
		// TODO Auto-generated method stub
		FragmentManager fm = getSupportFragmentManager();
		Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
		Bundle bundle = new Bundle();
		Log.d(TAG, "the selected date in create fragment = "+selectedDate);
		if (selectedDate!=null){
			bundle.putString(AppDayViewFragment.EXTRA_SELECTED_DATE, selectedDate);
			Log.d(TAG, "the selected date is not null and its value is "+selectedDate);
		} else {
			bundle.putString(AppDayViewFragment.EXTRA_SELECTED_DATE, day+"/"+month+"/"+year);
			Log.d(TAG, "the date being sent over is "+day+"/"+month+"/"+year);
			selectedDate = day+"/"+month+"/"+year;
		}
		if (fragment == null){
			fragment = new AppDayViewFragment();
			Log.d(saverDate, "right where the new fragment is created");

			fragment.setArguments(bundle);
			fm.beginTransaction().add(R.id.fragmentContainer, fragment).commitAllowingStateLoss();
		} else {
			fragment.getArguments().putString(AppDayViewFragment.EXTRA_SELECTED_DATE, selectedDate);
			Log.d(TAG, "Day: date in bundle "+bundle.getString(AppDayViewFragment.EXTRA_SELECTED_DATE));
			Log.d(TAG, "Calendar: date in bundle "+bundle.getString(AppCalendarViewFragment.EXTRA_SELECTED_DATE));
			//fm.beginTransaction().remove(fragment).commit();
			//fragment = new AppDayViewFragment();
			//fragment.setArguments(bundle);
			//fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
		}


		Log.d(TAG, "called create Fragment");

		//Fragment fragment = new AppCalendarViewFragment();
		//fragment.setArguments(bundle);
		//return fragment;
	}


	@Override
	public void onDateClickListenerCallBackFromCalendar(String date) {
		// TODO Auto-generated method stub
		Log.d(TAG, "call back called");
		selectedDate = date;
		saverDate = date;
	}


	@Override
	public void onDateClickListenerCallBackFromDayView(String date) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Call back from day view "+date);
		selectedDate = date;
		saverDate = date;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putString(SAVER_SELECTED_DATE, selectedDate);

		Log.d(TAG, "before: "+selectedDate);
	}

}
